﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HeresyManager.Comunication;

namespace HeresyManager.Careers
{
    [Serializable]
    public class TechPriest : Career
    {

        private Skill[] skills;
        private Talent[] talents;
        private Trait[] traits;

        public override string Name
        {
            get { return "Tech Priest"; }
        }

        public override IEnumerator<Comunicator> Acire(Character c)
        {

            List<Skill> skillList = new List<Skill>();

            skillList.Add(new Skills.SpeakLanguages.SpeakLanguageLowGothic(Skill.SkillLevel.Skilled));
            skillList.Add(new Skills.TechUse(Skill.SkillLevel.Skilled));
            skillList.Add(new Skills.Literacy(Skill.SkillLevel.Skilled));
            skillList.Add(new Skills.SecretTongue(Skill.SkillLevel.Skilled));
            var chooseSkill = new ChooseOne<Skill>("Choose one", new Skills.Trades.TradeScrimshawer(Skill.SkillLevel.Skilled), new Skills.Trades.TradeCopyist(Skill.SkillLevel.Skilled));

            while (chooseSkill.SelectedValue == null)
                yield return chooseSkill;
            skillList.Add(chooseSkill.SelectedValue);


            skills = skillList.ToArray();

            foreach (var s in skills)
            {
                var en = c.AddSkill(s, this);
                while (en.MoveNext())
                    yield return en.Current;
            }




            List<Talent> talentList = new List<Talent>();
            talentList.Add(new Talents.WeaponTraining.MeleeWeaponTrainingPrimitive());
            talentList.Add(new Talents.WeaponTraining.BasicWeaponTrainingLas());
            talentList.Add(new Talents.WeaponTraining.PistolTrainingLas());
            talentList.Add(new Talents.ElectroGraftUse());

            var choose = new ChooseOne<Talent>("Choose One", new Talents.QuickDraw(), new Talents.RapidReload());
            while (choose.SelectedValue == null)
                yield return choose;
            talentList.Add(choose.SelectedValue);



            talents = talentList.ToArray();

            foreach (var s in talents)
            {
                var en = c.AddTalent(s, this);
                while (en.MoveNext())
                    yield return en.Current;
            }

            List<Trait> traitList = new List<Trait>();
            traitList.Add(new Traits.MechanicusImplants());


            traits = traitList.ToArray();

            foreach (var t in traits)
            {
                var en = c.AddTrait(t, this);
                while (en.MoveNext())
                    yield return en.Current;
            }

        }

        public override void DeAcire(Character c)
        {
            foreach (var s in skills)
            {
                c.RemoveSkill(s, this);
            }

            foreach (var item in talents)
            {
                c.RemoveTalent(item, this);
            }
            this.talents = null;
        }

        public override Rank[] Ranks
        {
            get { return new Rank[] { }; }
        }

        public override BackgroundPackage[] BackgroundPackages
        {
            get { return new BackgroundPackage[] { new TheDisciplesofThule(), new TheDivineLightofSollex(), new MalygrisianTechHeresy() }; }
        }

        #region BackgroundPackages

        [Serializable]
        private class TheDisciplesofThule : BackgroundPackage
        {

            private Skill[] skills;



            public override string Name
            {
                get { return "The Disciples of Thule"; }
            }

            public override int Cost
            {
                get { return 100; }
            }

            public override bool IsPreconditionSattisfied(Character c)
            {
                return base.IsPreconditionSattisfied(c) && c.HomeWorld is HomeWorlds.VoidBorn || c.HomeWorld is HomeWorlds.ForgeWorld;
            }

            public override IEnumerator<Comunicator> Accire(Character c)
            {
                var en = base.Accire(c);
                while (en.MoveNext())
                    yield return en.Current;

                c.Per -= 5;
                c.WS -= 5;





                skills = new Skill[] { new Skills.CommonLores.CommonLoreMachineCult(Skill.SkillLevel.Skilled), new Skills.Evaluate(Skill.SkillLevel.Skilled), new Skills.ForbiddenLores.ForbiddenLoreArcheotech(Skill.SkillLevel.Skilled), new Skills.Logic(Skill.SkillLevel.Skilled), new Skills.ScholasticLores.ScholasticLoreCryptology(Skill.SkillLevel.Skilled) };
                foreach (var s in skills)
                {
                    en = c.AddSkill(s, this);
                    while (en.MoveNext())
                        yield return en.Current;
                }



            }

            public override void DeAccire(Character c)
            {
                base.DeAccire(c);
                foreach (var s in skills)
                {
                    c.RemoveSkill(s, this);
                }

                c.Per += 5;
                c.WS += 5;
            }
        }

        [Serializable]
        private class TheDivineLightofSollex : BackgroundPackage
        {

            private Skill[] skills;

            private Talent[] talent;


            private int insanety;

            public override string Name
            {
                get { return "The Divine Light of Sollex"; }
            }

            public override int Cost
            {
                get { return 300; }
            }

            public override bool IsPreconditionSattisfied(Character c)
            {
                return base.IsPreconditionSattisfied(c) && c.HomeWorld is HomeWorlds.ForgeWorld;
            }

            public override IEnumerator<Comunicator> Accire(Character c)
            {
                var en = base.Accire(c);
                while (en.MoveNext())
                    yield return en.Current;

                c.Fel -= 5;

                talent = new Talent[] { new Talents.Hatreds.HatredHeretics(), new Talents.UnshakeabkleFaith() };
                foreach (var t in talent)
                {
                    en = c.AddTalent(t, this);
                    while (en.MoveNext())
                        yield return en.Current;
                }

                skills = new Skill[] { new Skills.Cipherses.CiphersSecretSociety(Skill.SkillLevel.Skilled), new Skills.CommonLores.CommonLoreMachineCult(Skill.SkillLevel.Skilled), new Skills.Demolition(Skill.SkillLevel.Skilled), new Skills.ScholasticLores.ScholasticLoreTacticaImperialis(Skill.SkillLevel.Skilled) };
                foreach (var s in skills)
                {
                    en = c.AddSkill(s, this);
                    while (en.MoveNext())
                        yield return en.Current;
                }

                //TODO Replace Gear

                var rolDie = new RoleDice("Insanaty", Dice.D5);
                while (rolDie.Value == -1)
                    yield return rolDie;
                insanety = rolDie.Value;

                c.InsanityPoints += insanety;
            }

            public override void DeAccire(Character c)
            {
                base.DeAccire(c);
                foreach (var t in talent)
                {
                    c.RemoveTalent(t, this);
                }
                foreach (var s in skills)
                {
                    c.RemoveSkill(s, this);
                }
                c.Fel += 5;

                c.InsanityPoints -= insanety;

            }
        }

        [Serializable]
        private class MalygrisianTechHeresy : BackgroundPackage
        {

            private Skill[] skills;



            private int insanety;
            private int coruption;

            public override string Name
            {
                get { return "Malygrisian Tech Heresy"; }
            }

            public override int Cost
            {
                get { return 200; }
            }

            public override bool IsPreconditionSattisfied(Character c)
            {
                return base.IsPreconditionSattisfied(c) && c.HomeWorld is HomeWorlds.ForgeWorld || c.HomeWorld is HomeWorlds.VoidBorn;
            }

            public override IEnumerator<Comunicator> Accire(Character c)
            {
                var en = base.Accire(c);
                while (en.MoveNext())
                    yield return en.Current;




                skills = new Skill[] { new Skills.Cipherses.CiphersMalygrisCodex(Skill.SkillLevel.Skilled), new Skills.ForbiddenLores.ForbiddenLoreWarp(Skill.SkillLevel.Skilled), new Skills.ForbiddenLores.ForbiddenLoreXenos(Skill.SkillLevel.Skilled), new Skills.ScholasticLores.ScholasticLoreLegend(Skill.SkillLevel.Skilled), new Skills.ScholasticLores.ScholasticLoreNumerology(Skill.SkillLevel.Skilled) };
                foreach (var s in skills)
                {
                    en = c.AddSkill(s, this);
                    while (en.MoveNext())
                        yield return en.Current;
                }

                //TODO Replace Gear

                var rolDie = new RoleDice("Inenety", Dice.D5);
                while (rolDie.Value == -1)
                    yield return rolDie;
                insanety = rolDie.Value;

                c.InsanityPoints += insanety;

                rolDie = new RoleDice("Coruption", Dice.D5);
                while (rolDie.Value == -1)
                    yield return rolDie;
                coruption = rolDie.Value;

                c.CorruptionPoints += coruption;
            }

            public override void DeAccire(Character c)
            {
                base.DeAccire(c);

                foreach (var s in skills)
                {
                    c.RemoveSkill(s, this);
                }


                c.InsanityPoints -= insanety;
                c.CorruptionPoints -= coruption;

            }
        }
        #endregion


        public override int[] WeaponSkillCost
        {
            get { return new int[] { 250, 500, 750, 1000 }; }
        }

        public override int[] BalisticSkillCost
        {
            get { return new int[] { 250, 500, 750, 1000 }; }
        }

        public override int[] StrengthCost
        {
            get { return new int[] { 500, 750, 1000, 2500 }; }
        }

        public override int[] ToughnessCost
        {
            get { return new int[] { 100, 250, 500, 750 }; }
        }

        public override int[] AgilityCost
        {
            get { return new int[] { 500, 750, 1000, 2500 }; }
        }

        public override int[] IntelligenceCost
        {
            get { return new int[] { 100, 250, 500, 750 }; }
        }

        public override int[] PerceptionCost
        {
            get { return new int[] { 250, 500, 750, 1000 }; }
        }

        public override int[] WillpowerCost
        {
            get { return new int[] { 100, 250, 500, 750 }; }
        }

        public override int[] FellowshipCost
        {
            get { return new int[] { }; }
        }
    }
}
